import { legacy_createStore as createStore, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension"; // chrome 浏览器插件
import reducer from "./reducer";
import thunk from "redux-thunk";
import createSagaMiddleware from "redux-saga"; // 获取异步 action
import logger from "redux-logger";
import TodoSagas from "../sagas";

import { persistReducer, persistStore } from "redux-persist"; // redux-persist目的用来做数据本地持久化存储，persistReducer persistStore，分别用来配置保存的数据源和保存数据的方法 
import storage from "redux-persist/lib/storage"; // 导入需要配置的数据源，可以选择，storage，cookie,session等

//定义配置的信息
const persitConfig = {
  key: "root",
  storage: storage,
  // 如果不想将部分state持久化，可以将其放入黑名单(blacklist)中.黑名单是设置
  // blacklist: ['ll']
};

const persist_reducers = persistReducer(persitConfig, reducer); // 创建持久化的配置persist的信息

const sagaMiddleware = createSagaMiddleware();

// 创建一个公共存储仓库
const store = createStore(
  persist_reducers, // 初始化数据
  composeWithDevTools(applyMiddleware(thunk, sagaMiddleware, logger)) // 使用redux中间件
);
sagaMiddleware.run(TodoSagas);

const persistor = persistStore(store);

export { store, persistor };
